/**
 * @author yusak
 */
ChaShaKu.SequenceCenterListSearch = Ext.extend(Ext.app.Module,{
	id: 'sequenceCenterListSearch',
	run: function(){
		this.app.modules.advancedSearchPanel.beforeRenderModules.push(this);
	},
	create: function(){
		var panel = this.app.modules.advancedSearchPanel;
		var grid = panel.grid;
		var ds = grid.getStore();
		var target = this.app.viewport.mainPanel;
		var field = this.app.viewport.field;
		
		var search = function(btn,e){
			ds.filterBy(Ext.app.InputValidator);
			var records = ds.getRange();
			var data = {
				keywords: records.map(Ext.app.InputEncoder.encodeParams),
				locations: records.map(Ext.app.InputEncoder.getLocation),
				center_word: Ext.app.InputEncoder.getCenterWordIndex(records)
			};
			
			var grid_ds = Ext.app.Data.ListStore.create({url: 'optimized_search/list_by_word_sequence'},{keywords:Ext.util.JSON.encode(data),center_uniq:'check'});
			grid_ds.on('beforeload',function(store,options){
				if(store.getTotalCount() > 0){
					options.params['total'] = store.getTotalCount();
				}
			});
			
			var p = target.add(Ext.app.Grid.SequenceCenterList.create('Advanced Search Results',grid_ds,data.keywords));
			panel.dialog.hide();
			target.setActiveTab(p);
			grid_ds.load({params:{start:0, limit:25}});
			
//			field.reset();
			panel.onReady = false;
		}
		var btn = panel.dialog.addButton({
			text:'Make a word list (center word)',
			disabled: true
		},search,this);
		panel.searchBtns.push(btn);
	}
});